Thread: [HELP] Do while loop with spacebar?

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    3

    [HELP] Do while loop with spacebar?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main ()
    {
        char c;
        do{
        printf("test ");
        scanf(" %c",&c);
    
    
        }while(c=='\x20');
    
    
        return 0;
    }
    This is what I got so far. The spacebar wont trigger the loop. It works when I assign other keys but only the spacebar thats messing with me. I've also tried with c = getchar() but it makes the program only run once. Does anyone know how to fix this? Thanks beforehand.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > while(c=='\x20')
    Try it without the single quotation marks.

    You should have got a warning about large character constants with them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    3
    Do you mean like while(c==\x20) cuz that does not work for me.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    0x20
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    3
    This worked when i removed the space in scanf(" %c",&c); >> scanf("%c",&c); problem now is that it does the same as getchar() and before with normal space that it only loop once and does not ask scanf again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. Ignoring the Spacebar
    By rehan in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2007, 12:56 AM
  4. spacebar problem?
    By limitmaster in forum Tech Board
    Replies: 2
    Last Post: 07-17-2006, 07:07 AM
  5. Pressed Spacebar
    By smonk in forum Windows Programming
    Replies: 4
    Last Post: 08-02-2005, 04:07 PM

Tags for this Thread